package com.jsgt.greekfire.ui.trading.otc

import android.content.res.Resources
import android.graphics.Typeface
import android.util.TypedValue
import android.widget.LinearLayout
import android.widget.TextView
import androidx.viewpager.widget.ViewPager
import com.fhx.core.base.BaseActivity
import com.fhx.core.base.BaseFragment
import com.google.android.material.tabs.TabLayout
import com.jsgt.greekfire.R
import com.jsgt.greekfire.ui.trading.adapter.PagerAdapter
import kotlinx.android.synthetic.main.activity_commission.*

class OTCCommissionActivity : BaseActivity() {
    private val fragments: MutableList<BaseFragment> = ArrayList()
    private var coinFragment: OTCCommissionFragment? = null
    var tvTitle: TextView? = null

    override fun getLayoutId(): Int = R.layout.activity_commission

    override fun main() {

        initTab()
        tvTitle = findViewById(R.id.title_tv)
        tvTitle?.text = "我的订单"
    }

    private fun initTab() {
        var item = resources.getStringArray(R.array.historical_orders_list1)
        val tabs = java.util.ArrayList<String>()
        for (i in item.indices) {
            tabs.add(item[i])
            coinFragment = OTCCommissionFragment.newInstance(i)
            fragments.add(coinFragment!!)
        }

        tabCommission.tabMode = TabLayout.MODE_FIXED
        tabCommission.setupWithViewPager(vpCommission)
        vpCommission.adapter = PagerAdapter(
            supportFragmentManager,
            fragments,
            tabs
        )
        val title =
            ((tabCommission.getChildAt(0) as LinearLayout).getChildAt(0) as LinearLayout).getChildAt(
                1
            ) as TextView

        title.apply {
            setTextAppearance(context, R.style.TabLayoutTextStyle)
            setTextColor(context.resources.getColor(R.color.otc_order_tab_777777))
            setTypeface(Typeface.defaultFromStyle(Typeface.BOLD))
        }
        vpCommission.currentItem = 0
        vpCommission.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
            }
        })
        tabCommission.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {

            override fun onTabSelected(tab: TabLayout.Tab?) {
                var tabIndex = tab?.position ?: 0
                var pageIndex = 1

                val title =
                    ((tabCommission.getChildAt(0) as LinearLayout).getChildAt(tab!!.position) as LinearLayout).getChildAt(
                        1
                    ) as TextView

                title.apply {
                    setTextAppearance(context, R.style.TabLayoutTextStyle)
                    setTextColor(context.resources.getColor(R.color.otc_order_tab_777777))
                    setTypeface(Typeface.defaultFromStyle(Typeface.BOLD))
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                val title =
                    ((tabCommission.getChildAt(0) as LinearLayout).getChildAt(tab!!.position) as LinearLayout).getChildAt(
                        1
                    ) as TextView
                title.apply {
                    setTextAppearance(context, R.style.TabLayoutTextStyle)
                    setTextColor(context.resources.getColor(R.color.otc_order_tab_777777))
                    setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL))
                }
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

        })
    }


}